package com.wkedu.repair.controller;

import com.wkedu.repair.dto.*;
import com.wkedu.repair.entity.Supervisor;
import com.wkedu.repair.enums.CodeEnum;
import com.wkedu.repair.exception.LoginException;
import com.wkedu.repair.form.SupervisorLoginForm;
import com.wkedu.repair.form.SupervisorSignForm;
import com.wkedu.repair.form.SupervisorStudentForm;
import com.wkedu.repair.form.SupervisorTeacherForm;
import com.wkedu.repair.reponse.Response;
import com.wkedu.repair.service.StudentService;
import com.wkedu.repair.service.SupervisorService;
import com.wkedu.repair.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/9/5 3:23 下午
 */
@RestController
@CrossOrigin
@RequestMapping("/supervisor")
public class SupervisorController {

    @Autowired
    private SupervisorService supervisorService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/login")
    public Response SupervisorLogin(@RequestBody SupervisorLoginForm supervisorLoginForm) throws LoginException {
        Response<String> response = new Response<>();
        SupervisorDto supervisorDto = supervisorService.getSupervisorForUsername(supervisorLoginForm.getUsername());

        if (supervisorDto != null){
            if (supervisorLoginForm.getPassword().equals(supervisorDto.getPassword())) {
                response.setCode(CodeEnum.SUCCESS.getCode());
            }else {
                response.setCode(CodeEnum.ERROR.getCode());
                throw new LoginException("mima密码错误");
            }
        }else {
            response.setCode(CodeEnum.ERROR.getCode());
//            throw new LoginException(CodeEnum.ERROR.getMessage());
            response.setData(null);
        }


        return response;
    }

    @PostMapping("/sign")
    public Response Supervisor(@RequestBody SupervisorSignForm supervisorSignForm){
        Response<String> response = new Response<>();

        SupervisorDto supervisorDto1 = supervisorService.getSupervisor(supervisorSignForm.getSupervisorId());
        SupervisorDto supervisorDto2 = supervisorService.getSupervisorForUsername(supervisorSignForm.getUsername());

        if (supervisorDto1 == null && supervisorDto2 == null){
            SupervisorDto supervisorDto = new SupervisorDto(
                    supervisorSignForm.getName(),
                    supervisorSignForm.getUsername(), supervisorSignForm.getPhone(),
                    supervisorSignForm.getPhone(), supervisorSignForm.getSupervisorId()
            );

            supervisorService.addSupervisor(supervisorDto);

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData("主管注册成功");
        }else {
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData("已有该用户");
        }




        return response;


    }

    @GetMapping("/get_supervisor")
    public Response getSupervisor(@RequestParam String username){
        Response<SupervisorDto> supervisorDtoResponse = new Response<>();

        SupervisorDto supervisorForUsername = supervisorService.getSupervisorForUsername(username);

        if (supervisorDtoResponse != null) {

            supervisorDtoResponse.setCode(CodeEnum.SUCCESS.getCode());
            supervisorDtoResponse.setData(supervisorForUsername);

        }else {
            supervisorDtoResponse.setCode(CodeEnum.ERROR.getCode());
            supervisorDtoResponse.setData(null);
        }






        return supervisorDtoResponse;
    }


    @GetMapping("/list_student")
    public Response RepairStudentList(){
        Response<List<SupervisorStudentForm>> response = new Response<>();
        List<SupervisorStudentForm> supervisorStudentForms = new ArrayList<>();
        List<RepairStudentDto> repairStudentDtoList = supervisorService.listRepairStudentDto();
        
        if ( !repairStudentDtoList.isEmpty() ) {

            for (RepairStudentDto r :
                    repairStudentDtoList) {
                StudentDto studentForStudentId = studentService.getStudentForStudentId(r.getStudentId());
                if (studentForStudentId != null){
                    SupervisorStudentForm supervisorStudentForm = new SupervisorStudentForm(r.getRepairInfo(),
                            r.getRepairPath(), r.getRepairType(), r.getStudentId(), studentForStudentId.getName());
                    supervisorStudentForms.add(supervisorStudentForm);
                }
            }

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(supervisorStudentForms);
            
        }else {
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData(null);
        }


        return response;
    }


    @GetMapping("/list_teacher")
    public Response RepairTeacherList(){
        Response<List<SupervisorTeacherForm>> response = new Response<>();
        List<SupervisorTeacherForm> supervisorTeacherFormList = new ArrayList<>();

        List<RepairTeacherDto> repairTeacherDtoList = supervisorService.listRepairTeacherDto();

        if ( !repairTeacherDtoList.isEmpty() ) {

            for (RepairTeacherDto r :
                    repairTeacherDtoList) {
                TeacherDto teacherForTeacherId = teacherService.getTeacherForTeacherId(r.getTeacherId());

                if (teacherForTeacherId != null ){
                    SupervisorTeacherForm supervisorTeacherForm = new SupervisorTeacherForm(r.getRepairInfo(),
                            r.getRepairPath(), r.getRepairType(), r.getTeacherId(), teacherForTeacherId.getName());
                    supervisorTeacherFormList.add(supervisorTeacherForm);
                }

            }

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(supervisorTeacherFormList);
        }else {
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData(null);
        }


        return response;
    }


}
